package com.cupcorn.lib.game;

import org.andengine.entity.shape.RectangularShape;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import com.cupcorn.lib.Anchor;
import com.cupcorn.lib.Ratio;
import com.cupcorn.lib.Utils;

public class GameSprite extends Sprite
{
	public GameRectangle	rectangle;
	public GameText				text;
	Ratio ratio;
	
	public GameSprite(float pX, float pY, float pWidth, float pHeight, ITextureRegion pTextureRegion, VertexBufferObjectManager pVertexBufferObjectManager, Ratio ratio)
	{
		super(pX, pY, pWidth, pHeight, pTextureRegion, pVertexBufferObjectManager);
		this.ratio = ratio;
	}
	
	public void setPosition(float pX, float pY, Anchor anchor, Ratio ratio)
	{
		super.setPosition(Utils.getPosX(pX, this.mWidth, anchor, ratio), Utils.getPosY(pY, mHeight, anchor, ratio));
	}
	
	public void destroy()
	{
		this.detachSelf();
		this.getTextureRegion().getTexture().unload();
	}
	
	public void addRectangle()
	{
		rectangle = new GameRectangle(0, 0, mWidth, mHeight, getVertexBufferObjectManager());
		attachChild(rectangle);
	}
	
	public void addRectangle(GameRectangle rectangle)
	{
		this.rectangle = rectangle;
		rectangle.setPosition(Utils.getCenter(mWidth, rectangle.getWidthScaled()), Utils.getCenter(mHeight, rectangle.getHeightScaled()));
		attachChild(rectangle);
	}
	
	public void addText(GameText text)
	{
		this.text = text;
		text.setPosition(Utils.getCenter(mWidth, text.getWidthScaled()), Utils.getCenter(mHeight, text.getHeightScaled()));
		attachChild(text);
	}
	
	public void addText(GameText text, float x, float y, Anchor anchor)
	{
		this.text = text;
		float _x = Utils.getPosX(x, text.getWidthScaled(), anchor, this.ratio, this);
		float _y = Utils.getPosY(y, text.getHeightScaled(), anchor, this.ratio, this); 
		text.setPosition(_x, _y);
		attachChild(text);
	}
	
	public void setTextPosition(GameText text, float x, float y, Anchor anchor)
	{
		float _x = Utils.getPosX(x, text.getWidthScaled(), anchor, this.ratio, this);
		float _y = Utils.getPosY(y, text.getHeightScaled(), anchor, this.ratio, this); 
		text.setPosition(_x, _y);
	}	
	
	public void addRectangularShape(RectangularShape rectShape, float x, float y, Anchor anchor)
	{
		float _x = Utils.getPosX(x, rectShape.getWidthScaled(), anchor, this.ratio, this);
		float _y = Utils.getPosY(y, rectShape.getHeightScaled(), anchor, this.ratio, this); 
		rectShape.setPosition(_x, _y);
		attachChild(rectShape);
	}
	
	public void addRectangularShape(RectangularShape rectShape, Anchor anchor)
	{
		addRectangularShape(rectShape, 0, 0, anchor);
	}
}
